package com.dataseek.iot.product.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.product.entity.PdtAttrRel;
import com.dataseek.iot.product.service.PdtAttrRelService;
import com.dataseek.iot.product.vo.PdtAttrRelVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/pdtAttr")
public class PdtAttrRelController {

    @Autowired
    private PdtAttrRelService pdtAttrRelService;


    /**
     * 查询产品属性定义列表
     *
     * @param pdtCode 产品属性定义名称
     * @return
     */

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult queryPdtAttrRel(@RequestParam(value="page")  Integer page, @RequestParam(value="size")  Integer size, @RequestParam(value="pdtCode") String pdtCode ) throws IOException {
        PdtAttrRelVo pdtListVo = pdtAttrRelService.queryPdtAttrRel(pdtCode,page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),pdtListVo);
    }

    /**
     * 修改产品属性定义列表
     *
     * @param pdtList 产品属性定义实体类
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult updatePdtAttrRel(@RequestBody PdtAttrRel pdtList) throws IOException {
        Integer code  = pdtAttrRelService.updatePdtAttrRel(pdtList);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult = new ResponseResult(RspStatusEnum.FAIL.getCode(),"产品属性定义编码字段重复，请重新输入!",code.toString());
        } else {
            responseResult = new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
        }
        return responseResult;
    }

    /**
     * 新建产品属性定义
     *
     * @param pdtList 产品属性定义实体类
     * @return
     */

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insertPdtAttrRel(@RequestBody PdtAttrRel pdtList) throws IOException, SQLException {
        Integer code  = pdtAttrRelService.insertPdtAttrRel(pdtList);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult = new ResponseResult(RspStatusEnum.FAIL.getCode(),"产品属性定义编码字段重复，请重新输入!",code.toString());
        } else {
            responseResult = new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
        }
        return responseResult;
    }


    /**
     * 删除产品属性定义
     *
     * @param ids 产品属性定义ID,满足批量删除
     * @return
     */

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult deletePdtAttrRel(@RequestBody JSONObject ids)  throws IOException {
        JSONArray jsonArray = ids.getJSONArray("ids");
        List<Long> idList = JSONObject.parseArray(jsonArray.toJSONString(), Long.class);
        Integer code  = pdtAttrRelService.deletePdtAttrRel(idList);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }



}
